package me.work.pay.jsyl.mvp.model;

import android.app.Application;

import com.google.gson.Gson;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import java.util.Map;

import javax.inject.Inject;

import dagger.Module;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import io.rx_cache2.DynamicKey;
import io.rx_cache2.EvictDynamicKey;
import me.work.pay.jsyl.mvp.contract.GroupListContract;
import me.work.pay.jsyl.mvp.model.api.CommonApi;
import me.work.pay.jsyl.mvp.model.api.cache.CommonCache;
import me.work.pay.jsyl.mvp.model.entity.LauncherData;
import me.work.pay.jsyl.mvp.model.entity.UpdateInfo;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 10/28/2019 13:20
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@Module
public class GroupListModel extends BaseModel implements GroupListContract.Model {
    @Inject
    Gson mGson;
    @Inject
    Application mApplication;

    @Inject
    public GroupListModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Observable<LauncherData> getGroup(Map<String, Object> map, boolean available) {
        return Observable.just(mRepositoryManager.obtainRetrofitService(CommonApi.class).launcher(map))
                .flatMap((Function<Observable<LauncherData>, ObservableSource<LauncherData>>)
                        listObservable -> mRepositoryManager.obtainCacheService(CommonCache.class)
                                .launcher(listObservable
                                        , new DynamicKey(map.get("sn"))
                                        , new EvictDynamicKey(available))
                                .map(listReply -> listReply.getData()));

    }

    @Override
    public Observable<String> upload_log(Map<String, Object> map) {
        return mRepositoryManager.obtainRetrofitService(CommonApi.class).uploadlogtoaliyun(map);
    }

    @Override
    public Observable<UpdateInfo> check_version(String appVersionName) {
        return mRepositoryManager.obtainRetrofitService(CommonApi.class).check_version(appVersionName,"ylbapp");
    }

}